package cn.com.inossem.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.util.Base64;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.activity.ImageViewDialog;
import cn.com.inossem.activity.R;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.model.BaGuaListInfoBean;
import cn.com.inossem.utils.AsyncImageLoader;
import cn.com.inossem.utils.AsyncImageLoader.ImageCallback;
import cn.com.inossem.utils.Utils;

public class BaGuaDetailsAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private BaGuaListInfoBean messageInfo;
	List<BaGuaListInfoBean> retList;
	private Handler handler;
	int size = 8;
	int total;
	EditText editText;

	public void setTotal(int total) {
		this.total = total;
	}

	public BaGuaDetailsAdapter(Context mContext, BaGuaListInfoBean messageInfo,
			List<BaGuaListInfoBean> retList, Handler handler) {
		super();
		this.mContext = mContext;
		this.messageInfo = messageInfo;
		this.handler = handler;
		this.retList = retList;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		// return messageInfo.size();
		return retList.size() + 1;
	}

	@Override
	public Object getItem(int arg0) {
		// return messageInfo.get(arg0);
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (position == 0) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.baguadetails_item_layout,
					null);
			holder.pic = (ImageView) convertView.findViewById(R.id.pic);
			holder.llayout = (LinearLayout) convertView
					.findViewById(R.id.llayout);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.createTime = (TextView) convertView
					.findViewById(R.id.createTime);
			holder.isFriendPraise = (TextView) convertView
					.findViewById(R.id.isFriendPraise);
			holder.praseCount = (TextView) convertView
					.findViewById(R.id.praseCount);
			holder.dianzanlayout = (LinearLayout) convertView
					.findViewById(R.id.dianzanlayout);
			holder.total = (TextView) convertView.findViewById(R.id.total);
			holder.dianzan = (ImageView) convertView.findViewById(R.id.dianzan);
			holder.gridview = (GridView) convertView
					.findViewById(R.id.gridview);
			convertView.setTag(holder);
		} else if (position != 0) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(
					R.layout.baguadetails_huifuitem_layout, null);
			holder.pic = (ImageView) convertView.findViewById(R.id.pic);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.isFriendPraise = (TextView) convertView
					.findViewById(R.id.isFriendPraise);
			holder.praseCount = (TextView) convertView
					.findViewById(R.id.praseCount);
			holder.shanchu = (LinearLayout) convertView
					.findViewById(R.id.shanchu);
			holder.dianzan = (ImageView) convertView.findViewById(R.id.dianzan);
			convertView.setTag(holder);
		}

		editText = (EditText)parent.getRootView().findViewById(R.id.editText111);
		if (position == 0) {
			// holder.llayout.removeAllViews();
			holder.name.setText(messageInfo.getName());
			holder.text.setText(messageInfo.getText());
			holder.createTime.setText(messageInfo.getCreateTime());
			if (messageInfo.getIsFriendPraise().equals("0")) {
				holder.isFriendPraise.setText("好友的好友");
			} else {
				holder.isFriendPraise.setText("好友赞过");
			}
			if (!messageInfo.getIcon().equals("")
					&& messageInfo.getIcon().length() < 50) {
				holder.pic.setBackgroundResource(getResource(messageInfo
						.getIcon()));
			} else {
				holder.pic.setBackgroundDrawable(new BitmapDrawable(Utils
						.toRoundCorner(
								stringtoBitmap(Utils.messageInfo.userInfo
										.getImageData()), 50)));
			}

			holder.praseCount.setText(messageInfo.getPraseCount());
			// holder.total.setText(text);
			holder.total.setText(total + "");

			if (!messageInfo.getPicList().equals("")) {
				String[] pic = { "" };
				if (messageInfo.getPicList().contains(",")) {
					pic = messageInfo.getPicList().split(",");
				} else {
					pic[0] = messageInfo.getPicList();
				}
				int dipheight = 0;
				if (pic.length < 4) {
					dipheight = 90;
				} else if (pic.length < 7) {
					dipheight = 180;
				} else {
					dipheight = 270;
				}
				LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) holder.gridview
						.getLayoutParams();
				params.height = Utils.dip2px(mContext, dipheight);
				holder.gridview.setLayoutParams(params);

				holder.gridview.setAdapter(new ImageAdapter(mContext,
						holder.gridview, messageInfo.getPicList()));
				holder.gridview.setAdapter(new ImageAdapter(mContext,
						holder.gridview, messageInfo.getPicList()));
			}
			// addView(holder.llayout);

			if (messageInfo.getIsMePraise().equals("0")) {
				holder.dianzan.setBackgroundResource(R.drawable.dianzan);
			} else {
				holder.dianzan.setBackgroundResource(R.drawable.dianzan1);
			}
			holder.dianzanlayout.setTag(holder);
			holder.dianzanlayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					ViewHolder hold = (ViewHolder) arg0.getTag();
					if (Utils.isDenglu == 0) {
						Toast.makeText(mContext, "请登录！", Toast.LENGTH_LONG)
								.show();
					} else if (messageInfo.getIsMePraise().equals("0")) {
						System.out.println("getItem(position).getIsMePraise():"
								+ messageInfo.getIsMePraise() + "111");
						messageInfo.setIsMePraise("1");
						messageInfo.setPraseCount((Integer.parseInt(messageInfo
								.getPraseCount()) + 1) + "");
						Toast.makeText(mContext, "点赞成功！", Toast.LENGTH_LONG)
								.show();
						hold.dianzan.setBackgroundResource(R.drawable.dianzan1);
						Message me = new Message();
						me.what = 1;
						Bundle bun = new Bundle();
						bun.putString("id", messageInfo.getId());
						me.setData(bun);
						handler.sendMessage(me);
					} else {
						System.out.println("getItem(position).getIsMePraise():"
								+ messageInfo.getIsMePraise() + "222");
						messageInfo.setIsMePraise("0");
						messageInfo.setPraseCount((Integer.parseInt(messageInfo
								.getPraseCount()) - 1) + "");

						Toast.makeText(mContext, "取消点赞！", Toast.LENGTH_LONG)
								.show();
						hold.dianzan.setBackgroundResource(R.drawable.dianzan);
						Message me = new Message();
						me.what = 1;
						Bundle bun = new Bundle();
						bun.putString("id", messageInfo.getId());
						me.setData(bun);
						handler.sendMessage(me);
					}
				}
			});
			convertView.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					editText.setHint("");
				}
			});
		} else {
			if (!retList.get(position - 1).getIcon().equals("")
					&& retList.get(position - 1).getIcon().length() < 50) {
				holder.pic.setBackgroundResource(getResource(retList.get(
						position - 1).getIcon()));
			} else {
				if (!retList.get(position - 1).getIcon().equals(""))
					holder.pic.setBackgroundDrawable(new BitmapDrawable(Utils
							.toRoundCorner(
									stringtoBitmap(retList.get(position - 1)
											.getIcon()), 300)));
			}
			holder.name.setText(retList.get(position - 1).getName());
			holder.text.setText(retList.get(position - 1).getText());
			convertView.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					editText.setHint("回复" + retList.get(position - 1).getName() + ":");
				}
			});
			holder.dianzanlayout = (LinearLayout) convertView
					.findViewById(R.id.dianzanlayout);
			holder.praseCount
					.setText(retList.get(position - 1).getPraseCount());

			if (retList.get(position - 1).getIsMePraise().equals("0")) {
				holder.dianzan.setBackgroundResource(R.drawable.dianzan);
			} else {
				holder.dianzan.setBackgroundResource(R.drawable.dianzan1);
			}
			holder.dianzanlayout.setTag(holder);
			holder.dianzanlayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					ViewHolder hold = (ViewHolder) arg0.getTag();
					if (Utils.isDenglu == 0) {
						Toast.makeText(mContext, "请登录！", Toast.LENGTH_LONG)
								.show();
					} else if (retList.get(position - 1).getIsMePraise()
							.equals("0")) {
						retList.get(position - 1).setIsMePraise("1");
						retList.get(position - 1).setPraseCount(
								(Integer.parseInt(retList.get(position - 1)
										.getPraseCount()) + 1) + "");
						Toast.makeText(mContext, "点赞成功！", Toast.LENGTH_LONG)
								.show();
						hold.dianzan.setBackgroundResource(R.drawable.dianzan1);
						Message me = new Message();
						me.what = 1;
						Bundle bun = new Bundle();
						bun.putString("id", retList.get(position - 1).getId());
						me.setData(bun);
						handler.sendMessage(me);
					} else {
						retList.get(position - 1).setIsMePraise("0");
						retList.get(position - 1).setPraseCount(
								(Integer.parseInt(retList.get(position - 1)
										.getPraseCount()) - 1) + "");

						Toast.makeText(mContext, "取消点赞！", Toast.LENGTH_LONG)
								.show();
						hold.dianzan.setBackgroundResource(R.drawable.dianzan);
						Message me = new Message();
						me.what = 1;
						Bundle bun = new Bundle();
						bun.putString("id", retList.get(position - 1).getId());
						me.setData(bun);
						handler.sendMessage(me);
					}
				}
			});
			if (retList.get(position - 1).getIsMeSend().equals("1")) {
				holder.shanchu.setVisibility(View.VISIBLE);
				holder.shanchu.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Message me = new Message();
						me.what = 2;
						Bundle bun = new Bundle();
						bun.putString("id", retList.get(position - 1).getId());
						me.setData(bun);
						handler.sendMessage(me);
					}
				});
			} else {
				holder.shanchu.setVisibility(View.GONE);
			}

		}

		return convertView;
	}

	public Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	/**
	 * 获取图片名称获取图片的资源id的方法
	 * 
	 * @param imageName
	 * @return
	 */
	public int getResource(String imageName) {
		int resId = mContext.getResources().getIdentifier(imageName,
				"drawable", mContext.getPackageName());
		return resId;
	}

	private void addView(LinearLayout llayout) {
		if (messageInfo.getPicList() == null) {
			return;
		}
		if (messageInfo.getPicList().equals("")) {
			return;
		}
		String[] pic = { "" };
		if (messageInfo.getPicList().contains(",")) {
			pic = messageInfo.getPicList().split(",");
		} else {
			pic[0] = messageInfo.getPicList();
		}
		LinearLayout llayoutItem = null;
		for (int i = 0; i < pic.length; i++) {
			if (i % 3 == 0) {
				llayoutItem = new LinearLayout(mContext);
				llayoutItem.setLayoutParams(new LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
				llayoutItem.setOrientation(LinearLayout.HORIZONTAL);
				llayoutItem.setGravity(Gravity.CENTER_HORIZONTAL);
				llayout.addView(llayoutItem);
			}
			final ImageView imageView = new ImageView(mContext);
			imageView.setPadding(5, 0, 5, 5);
			imageView.setLayoutParams(new LayoutParams(Utils.dip2px(mContext,
					80), Utils.dip2px(mContext, 80)));
			imageView.setTag(WSConfig.IMAGE_URL + pic[i]);
			imageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(mContext, ImageViewDialog.class);
					intent.putExtra("path", arg0.getTag().toString());
					mContext.startActivity(intent);

				}
			});
			AsyncImageLoader asyncImageLoader = new AsyncImageLoader(mContext);
			Drawable drawable = asyncImageLoader.loadDrawable(
					WSConfig.IMAGE_URL + pic[i], new ImageCallback() {
						@Override
						public void imageLoaded(Drawable imageDrawable,
								String imageUrl) {
							if (imageDrawable != null) {
								imageView.setBackgroundDrawable(imageDrawable);
							}
						}
					});
			if (drawable != null) {
				imageView.setBackgroundDrawable(drawable);
			}
			llayoutItem.addView(imageView);
		}

	}

	public final class ViewHolder {
		public ImageView pic;
		public LinearLayout llayout;
		public LinearLayout dianzanlayout;
		public LinearLayout shanchu;
		public TextView name;
		public TextView text;
		public TextView createTime;
		public TextView isFriendPraise;
		public TextView praseCount;
		public TextView total;
		public ImageView dianzan;
		public GridView gridview;
	}
}
